//index.js
//获取应用实例
var util = require('../../utils/util.js')
const app = getApp()
const bloodPressRecordUrl = require('../../config.js').bloodPressRecordUrl
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    time: "08:00",
    radioItems: [
      { name: '服药前', value: '0' },
      { name: '服药后', value: '1', checked: true }
    ],
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    //获取当前时间
    var now = util.formatTimeOnly(new Date)
    this.setData({
      time: now
    })

    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
  },
  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value);

    var radioItems = this.data.radioItems;
    for (var i = 0, len = radioItems.length; i < len; ++i) {
      radioItems[i].checked = radioItems[i].value == e.detail.value;
    }

    this.setData({
      radioItems: radioItems
    });
  },
  formSubmit:function(e){
    var token = wx.getStorageSync('token');
    var data = e.detail.value;
    if (e.detail.value.if_drug == 0){
      data['if_drug'] = "true"
    }else{
      data['if_drug'] = "false"
    }
    console.log(data);
    wx.request({
      url: bloodPressRecordUrl,
      data: e.detail.value,
      header : {
        'Authorization': "Token " + token
      },
      method: 'POST',
      success: function(res){
        wx.navigateBack({
          
        })
      }
    })
  }
})
